"""
球队相关 Schemas
"""
from pydantic import BaseModel, Field, ConfigDict
from typing import Optional, List
from datetime import datetime


class TeamBase(BaseModel):
    """球队基础信息"""
    name: str = Field(..., min_length=1, max_length=200)
    logo: Optional[str] = None
    description: Optional[str] = None
    maxMembers: int = Field(30, ge=1, le=100)
    cityId: Optional[str] = None  # 城市ID改为可选


class TeamCreate(TeamBase):
    """创建球队"""
    # captainId 和 captainName 由后端从 current_user 自动获取，不需要前端传递
    pass


class TeamUpdate(BaseModel):
    """更新球队"""
    name: Optional[str] = Field(None, min_length=1, max_length=200)
    logo: Optional[str] = None
    description: Optional[str] = None
    maxMembers: Optional[int] = Field(None, ge=1, le=100)
    cityId: Optional[str] = None
    cityName: Optional[str] = None  # 城市名称（前端会传递）
    foundedDate: Optional[str] = None


class TeamResponse(TeamBase):
    """球队响应"""
    id: str
    captainId: str
    captainName: str
    memberCount: int = 1
    createdAt: Optional[datetime] = None
    matchCount: int = 0
    winRate: float = 0.0
    cityName: Optional[str] = None  # 城市名称
    foundedDate: Optional[str] = None  # 建队日期
    
    model_config = ConfigDict(
        from_attributes=True,
        populate_by_name=True
    )


class TeamListQuery(BaseModel):
    """球队列表查询参数"""
    keyword: Optional[str] = None
    cityId: Optional[str] = None
    page: int = 1
    pageSize: int = 10


class KickMemberRequest(BaseModel):
    """踢出成员请求"""
    teamId: str
    userId: str


class RemoveMemberRequest(BaseModel):
    """移除成员请求"""
    teamId: str
    memberId: str


class TransferCaptainRequest(BaseModel):
    """转让队长请求"""
    teamId: str
    newCaptainId: str


class LeaveTeamRequest(BaseModel):
    """退出球队请求"""
    teamId: str


class JoinByCodeRequest(BaseModel):
    """通过邀请码加入请求"""
    inviteCode: str


class TeamMemberBase(BaseModel):
    """球队成员基础信息"""
    teamId: str
    userId: str
    userName: str
    avatar: Optional[str] = None
    positions: List[str] = []
    role: str = "member"


class TeamMemberResponse(BaseModel):
    """球队成员响应"""
    userId: str
    userName: str
    avatar: Optional[str] = None
    positions: List[str] = []
    role: str
    joinDate: Optional[datetime] = None
    matchCount: int = 0
    rating: float = 0.0
    
    model_config = ConfigDict(
        from_attributes=True,
        populate_by_name=True
    )
